﻿namespace DBUtil.Provider.MySql;

/// <summary>
/// MySql 数据库工厂类
/// </summary>
public class MySqlDBFactory : IDBFactory<MySqlAccess>
{
    /// <summary>
    /// 创建数据库操作对象
    /// </summary>
    /// <param name="DBConn">连接字符串</param>
    /// <param name="Settings">设置</param>
    /// <returns></returns>
    public static MySqlAccess CreateDB(string DBConn, DBSetting Settings)
    {
        if (string.IsNullOrWhiteSpace(DBConn)) return null;
        if (!DBConn.ToUpper().Contains("ALLOW USER VARIABLES") && !DBConn.ToUpper().Contains("ALLOWUSERVARIABLES"))
        {
            DBConn = DBConn.TrimEnd(';') + ";Allow User Variables=true;";
        }

        //if (!DBConn.ToUpper().Contains("ALLOW ZERO DATETIME") && !DBConn.ToUpper().Contains("ALLOWZERODATETIME"))
        //{
        //    DBConn = DBConn.TrimEnd(';') + ";Allow Zero DateTime=true;";
        //}
        Settings ??= DBSetting.NewInstance();
        if (Settings.Generator == null) Settings.SetIdSNOGenerator(new MySqlIdSNOGenerator());
        if (Settings.Locker == null) Settings.SetLocker(new MySqlLocker());
        return new MySqlAccess(DBConn, Settings);
    }

    /// <summary>
    /// 创建数据库操作对象
    /// </summary>
    /// <param name="DBType">数据库类型:MySql</param>
    /// <param name="DBConn">连接字符串:Data Source=localhost;Initial Catalog=test;User ID=root;Password=xxxx;</param>
    /// <param name="Settings">设置</param>
    /// <returns></returns>
    public MySqlAccess CreateDB(string DBType, string DBConn, DBSetting Settings)
    {
        DBType = (DBType ?? "").ToUpper();
        switch (DBType)
        {
            case Consts.ServerType:
                {
                    return CreateDB(DBConn, Settings);
                }
            default:
                {
                    return null;
                }
        }
    }
}

